package com.elitel.frame.base.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.alibaba.fastjson.JSON;
//import com.elitel.common.redis.RedisDbIndex;
//import com.elitel.common.redis.service.RedisCacheService;
import com.elitel.frame.base.component.RedisCatchBasicServer;
import com.elitel.frame.base.service.BasicServerService;
import com.elitel.frame.main.entity.BaseServices;
import com.elitel.frame.main.entity.ext.BasicServer;
import com.elitel.frame.main.entity.vo.PageResponseVo;

/**
 * 基本服务控制类
 *
 * @author dingdaifu
 */
@Api(value = "基本服务配置")
@Controller
@RequestMapping("/system/basicServer")
public class BasicServerController {


    @Resource
    private BasicServerService basicServerServiceImpl;

//	@Resource
//	private RedisCacheService redisCacheService;

    @Resource
    private RedisCatchBasicServer redisCatchBasicServer;

    private static final Logger logger = LoggerFactory.getLogger(BasicServerController.class);


    //private static final RedisDbIndex rdi =RedisDbIndex.ZERO;
    @RequestMapping(value = "/insertServer", consumes = "application/json")
    @ResponseBody
    public Map<String, Object> insertBasicServer(@RequestBody BasicServer u) {
//		int rdi = RedisDbIndex.value;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String servername = u.getServername();
            String serverurl = u.getServerurl();
            String tablename = u.getTablename();
            String remake = u.getRemake();
            String catched = u.getCatched();
            BaseServices basicserver = new BaseServices();
            basicserver.setLinktable(tablename);
            basicserver.setRemark(remake);
            basicserver.setServicename(servername);
            basicserver.setServiceurl(serverurl);
            String message = basicServerServiceImpl.insertBasicServer(basicserver);
            map.put("message", message);
//				String key  = "BASICSERVER";
				if ("success".equals(message)) {
					redisCatchBasicServer.refreshBasicServer();
				}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/deleteServer")
    public Map<String, Object> deleteServer(HttpServletRequest request) {
//		int rdi = RedisDbIndex.value;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String id = request.getParameter("keys");
            String message = basicServerServiceImpl.deleteServer(id);
            map.put("message", message);
//			String key  = "BASICSERVER";
			if ("success".equals(message)) {
				redisCatchBasicServer.refreshBasicServer();
			}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping(value = "/updateServer", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public Map<String, Object> updatetBasicServer(@RequestBody BasicServer basicServer) {
//		int rdi = RedisDbIndex.value;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String guid = basicServer.getGuid();
            String servername = basicServer.getServername();
            String serverurl = basicServer.getServerurl();
            String tablename = basicServer.getTablename();
            String remake = basicServer.getRemake();
            String catched = basicServer.getCatched();
            BaseServices basicserver = new BaseServices();
            basicserver.setGuid(guid);
            basicserver.setLinktable(tablename);
            basicserver.setRemark(remake);
            basicserver.setServicename(servername);
            basicserver.setServiceurl(serverurl);
            String message = basicServerServiceImpl.updateServer(basicserver);
            map.put("message", message);
//			String key  = "BASICSERVER";
			if ("success".equals(message)) {
				redisCatchBasicServer.refreshBasicServer();
			}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/selectServerOne")
    public BaseServices selectBaseServerOne(HttpServletRequest request) {
        BaseServices baseServices = null;
        try {
            String id = new String(request.getParameter("key").getBytes("iso-8859-1"), "utf-8");
            baseServices = basicServerServiceImpl.selectServerOne(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseServices;
    }

    @ResponseBody
    @RequestMapping("/paging")
    public PageResponseVo<BaseServices> selectBasicFenYe(HttpServletRequest request) {
//		int rdi = RedisDbIndex.value;
        PageResponseVo<BaseServices> pageResponseVo = null;
        try {
            String basicString = request.getParameter("seachTxt");
            String pageNo = new String(request.getParameter("pageNo").getBytes("iso-8859-1"), "utf-8");
            String pageSize = new String(request.getParameter("pageSize").getBytes("iso-8859-1"), "utf-8");
//			String key = "BASICSERVER";
            if ("".equals(basicString)) {
////				if (redisCacheService.stringExists(key, rdi)) {
////					pageResponseVo = redisCatchBasicServer.getRedisCatchBasicServer(key,pageNo,pageSize);
////				}else {
//					pageResponseVo = basicServerServiceImpl.selectBasicFenYe(pageNo,pageSize,basicString);
////					PageResponseVo<BaseServices> pageResponseVos = basicServerServiceImpl.selectBasicServerAll();
////					String basicServerString = JSON.toJSONString(pageResponseVos);
////					redisCacheService.saveObject(key, basicServerString, rdi);
////				}
                pageResponseVo = redisCatchBasicServer.getRedisCatchBasicServer(pageNo, pageSize);

            } else {
                pageResponseVo = basicServerServiceImpl.selectBasicFenYe(pageNo, pageSize, basicString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResponseVo;
    }

    /**
     * 查看guid在表中是否已经存在
     *
     * @param request
     * @return
     */
    @RequestMapping("/selectExit")
    @ResponseBody
    public int selectExit(HttpServletRequest request) {
        String guid = request.getParameter("dbKey");
        int count = basicServerServiceImpl.selectExit(guid);
        return count;
    }
}
